cmake_minimum_required(VERSION 3.18 FATAL_ERROR)

project(PiInfoPlugins)
set(CMAKE_FOLDER "PiInfo/Plugins")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/plugins")

include(CMakeParseArguments)
function(AddPlugin Name)
  set(options)
  set(oneValueArgs VERSION AUTHOR BRIEF DETAIL SRC_FOLDER)
  set(multiValueArgs RES_FOLDERS)
  cmake_parse_arguments(PLUGIN "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

  # check required values
  foreach(KEY ${oneValueArgs})
    if (NOT DEFINED PLUGIN_${KEY})
      message(FATAL_ERROR "AddPlugin Missing Required Argument ${KEY}")
    endif()
  endforeach()

  # extract version info
  string(REPLACE "." ";" VERSION_LIST ${PLUGIN_VERSION})
  list(LENGTH VERSION_LIST VERSION_CNT)
#  message(STATUS "Len: ${VERSION_CNT}")
  if (VERSION_CNT GREATER_EQUAL 1)
    # major
    list(GET VERSION_LIST 0 VERSION_MAJOR)
    math(EXPR VERSION_MAJOR "${VERSION_MAJOR} * 1" OUTPUT_FORMAT DECIMAL)
  endif()
  if (VERSION_CNT GREATER_EQUAL 2)
    # minor, default 0
    list(GET VERSION_LIST 1 VERSION_MINOR)
    math(EXPR VERSION_MINOR "${VERSION_MINOR} * 1" OUTPUT_FORMAT DECIMAL)
  else()
    set(VERSION_MINOR 0)
  endif()
  if (VERSION_CNT GREATER_EQUAL 3)
    # path, default 0
    list(GET VERSION_LIST 2 VERSION_PATCH)
    math(EXPR VERSION_PATCH "${VERSION_PATCH} * 1" OUTPUT_FORMAT DECIMAL)
  else()
    set(VERSION_PATCH 0)
  endif()

  # src info
  if (${CMAKE_SIZEOF_VOID_P} EQUAL 4)
    set(ARCH "32bit")
  else()
    set(ARCH "64bit")
  endif()
  if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
    set(COMPILER "gcc")
  elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    set(COMPILER "clang")
  elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
    set(COMPILER "msvc")
  else()
    set(COMPILER "unknown")
  endif()
  set(PLUGIN_SRC_INFO "${COMPILER}_${CMAKE_CXX_COMPILER_VERSION}_${ARCH}_git_${GIT_REVISION}")

  # generate plugin info file
  set(OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/${PLUGIN_SRC_FOLDER}/plugin_info.cpp)
  configure_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/plugin_info.cpp.cmakein
    ${OUTPUT_FILE}
  )
  source_group(TREE ${CMAKE_CURRENT_BINARY_DIR}/${PLUGIN_SRC_FOLDER} PREFIX "Generated Files" FILES ${OUTPUT_FILE})

  file(GLOB_RECURSE PLUGIN_SOURCES ${PLUGIN_SRC_FOLDER}/*.*)
  source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/${PLUGIN_SRC_FOLDER} FILES ${PLUGIN_SOURCES})

  list(APPEND PLUGIN_SOURCES ${OUTPUT_FILE})

  add_library(${Name} MODULE ${PLUGIN_SOURCES})
  target_strip(${Name})
  target_compile_definitions(${Name} PRIVATE -DPI_INFO_PLUGIN_BUILD)
  target_link_libraries(${Name} PRIVATE pi_sdk)

  # code gen for plugin
  generate_code(${Name})

  # process Resource
  if (DEFINED PLUGIN_RES_FOLDERS)
    # foreach(RES ${PLUGIN_RES_FOLDERS})
    #   add_resource(${Name} ${CMAKE_CURRENT_SOURCE_DIR}/${PLUGIN_SRC_FOLDER} ${RES})
    # endforeach()
    add_resource_folders(${Name} ${CMAKE_CURRENT_SOURCE_DIR}/${PLUGIN_SRC_FOLDER} ${PLUGIN_RES_FOLDERS})
    # add_dependencies(${Name} prcc)
  endif()

  set_target_properties(${Name} PROPERTIES PREFIX "" SUFFIX ".plugin")

  if (WIN32 AND TARGET PiInfoApp)
    add_custom_command(
      TARGET ${Name} POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:PiInfoApp>/plugins
      COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${Name}> $<TARGET_FILE_DIR:PiInfoApp>/plugins/
      COMMENT "copy plugin files"
    )
  endif()

endfunction()

AddPlugin(
        hefeng
        VERSION "1.0.0"
        AUTHOR "Guoli Tang"
        BRIEF "和风天气"
        DETAIL "天气情况展示，数据来源为和风天气"
        SRC_FOLDER hefeng_weather
        RES_FOLDERS assets layouts
)

AddPlugin(
        xxt
        VERSION "1.0.0"
        AUTHOR "Guoli Tang"
        BRIEF "行讯通公交信息"
        DETAIL "抓取/查看/搜索公交车信息"
        SRC_FOLDER xxt_bus
        RES_FOLDERS icons layouts test_images
)
